﻿using System.Collections.Generic;
using System.Linq;

namespace Basmus.ReadWrite
{
    public class ObjectElementsReaderWriter<TSourceValue> : IElementsReaderWriter<object>
    {
        private readonly IElementsReaderWriter<TSourceValue> _elementsReaderWriter;

        public ObjectElementsReaderWriter(IElementsReaderWriter<TSourceValue> elementsReaderWriter)
        {
            _elementsReaderWriter = elementsReaderWriter;
        }

        public IEnumerable<string> Write(IEnumerable<object> elements)
        {
            var sourceElements = elements == null ? null : elements.Select(x => (TSourceValue)x);
            var strings = _elementsReaderWriter.Write(sourceElements);
            return strings;
        }

        public IEnumerable<object> Read(IEnumerable<string> strings)
        {
            var sourceElements = _elementsReaderWriter.Read(strings);
            var targetElements = sourceElements == null ? null : sourceElements.Select(x => (object)x);
            return targetElements;
        }
    }
}